package cn.darkhorse.messystem.config;

import com.fhs.trans.service.impl.DictionaryTransService;
import com.ruoyi.common.core.domain.entity.SysDictData;
import com.ruoyi.system.service.ISysDictDataService;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.context.annotation.Configuration;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * trans服务 字典项自动翻译时使用
 */
@Configuration
public class DictionaryConfig implements InitializingBean {

    //注入字典翻译服务
    @Resource
    private DictionaryTransService dictionaryTransService;
    @Resource
    private ISysDictDataService iSysDictDataService;

    @Override
    public void afterPropertiesSet() throws Exception {
        List<SysDictData> sysDictData = iSysDictDataService.selectDictDataList(new SysDictData());
        Map<String, List<SysDictData>> collect = sysDictData.stream().collect(Collectors.groupingBy(SysDictData::getDictType));
        //将字典缓存刷新到翻译服务中
        collect.forEach((key,value)->{
            Map<String, String> dataMap = value.stream().collect(Collectors.toMap(SysDictData::getDictValue, SysDictData::getDictLabel));
            dictionaryTransService.refreshCache(key,dataMap);
        });
        //Map<String, String> dictionaryTransMap = dictionaryTransService.getDictionaryTransMap();
        //System.out.println(dictionaryTransMap);
    }
}
